package com.wx.example.demo.trywithresource;

import java.io.*;

/**
 * @author wangxin
 * @time 2018/7/23 17:17
 */
public class Test {


    public static void jdkEarly() throws FileNotFoundException, IOException{
        File file = new File("D:/export/readme.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        try {
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int _byte = fileInputStream.read();
        System.out.println(_byte);

        try {
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileInputStream.close();
        }
         _byte = fileInputStream.read();
        System.out.println(_byte);
    }

    public static void jdk7(){
        File file = new File("D:/export/readme.txt");
        try (FileInputStream fileInputStream = new FileInputStream(file)){
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void jdk9() throws FileNotFoundException, IOException{
        File file = new File("D:/export/readme.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream("D:/export/readme_backup.txt");

        try (fileInputStream; fileOutputStream) {
            fileInputStream.read();
            fileInputStream.transferTo(fileOutputStream);
            fileOutputStream.write(97);
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileOutputStream.write(65);
        fileInputStream.read();
    }


    public static void main(String[] args) throws FileNotFoundException, IOException {

        jdkEarly();
        jdk7();
        jdk9();

    }
}
